package com.xidian.zhiancloud.service.ledgerManager.targerAndSystem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.SecurityRewardPunishmentLedgerMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.SecurityRewardPunishmentLedger;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.targetAndSystem.SecurityRewardPunishmentLedgerQO;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityRewardPunishmentLedgerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:22
 */
@Service
public class SecurityRewardPunishmentLedgerService extends ServiceImpl<SecurityRewardPunishmentLedgerMapper, SecurityRewardPunishmentLedger> implements IService<SecurityRewardPunishmentLedger> {
    @Autowired
    private SecurityRewardPunishmentLedgerMapper securityRewardPunishmentLedgerMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<SecurityRewardPunishmentLedgerVO> selectListByQO(SecurityRewardPunishmentLedgerQO securityRewardPunishmentLedgerQO) {
        Page<SecurityRewardPunishmentLedger> page = new Page<>(securityRewardPunishmentLedgerQO.getCurrent(), securityRewardPunishmentLedgerQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        LambdaQueryWrapper<SecurityRewardPunishmentLedger> lambdaQuery = Wrappers.<SecurityRewardPunishmentLedger>lambdaQuery();
        lambdaQuery.eq(securityRewardPunishmentLedgerQO.getRewardPunishmentType() != null, SecurityRewardPunishmentLedger::getRewardPunishmentType, securityRewardPunishmentLedgerQO.getRewardPunishmentType())
                .eq(securityRewardPunishmentLedgerQO.getObjectType() != null, SecurityRewardPunishmentLedger::getObjectType, securityRewardPunishmentLedgerQO.getObjectType())
                .between(securityRewardPunishmentLedgerQO.getStartTime() != null && securityRewardPunishmentLedgerQO.getEndTime() != null, SecurityRewardPunishmentLedger::getCreateTime, securityRewardPunishmentLedgerQO.getStartTime(), securityRewardPunishmentLedgerQO.getEndTime())
                .eq(securityRewardPunishmentLedgerQO.getCompanyId() != null, SecurityRewardPunishmentLedger::getCompanyId, securityRewardPunishmentLedgerQO.getCompanyId());
        Page<SecurityRewardPunishmentLedger> ans = securityRewardPunishmentLedgerMapper.selectPage(page, lambdaQuery);
        return ans.convert(u -> {
            SecurityRewardPunishmentLedgerVO v = new SecurityRewardPunishmentLedgerVO();
            BeanUtils.copyProperties(u, v);
            if(u.getObjectType() != null){
                if (u.getObjectType() == 0) {
                    Organization organization = organizationMapper.selectById(u.getObjectId());
                    v.setObjectName(organization == null ? "该部门已被删除" : organization.getName());
                } else if(u.getObjectType() == 1){
                    SysUser user = sysUserMapper.selectById(u.getObjectId());
                    v.setObjectName(user == null ? "该用户已被删除" : user.getName());
                }
            }
            return v;
        });
    }

    public SecurityRewardPunishmentLedgerVO getVOById(Long id) {
        SecurityRewardPunishmentLedger ledger = securityRewardPunishmentLedgerMapper.selectById(id);
        if (ledger == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        SecurityRewardPunishmentLedgerVO vo = new SecurityRewardPunishmentLedgerVO();
        BeanUtils.copyProperties(ledger, vo);
        if (ledger.getObjectType() == 0) {
            Organization organization = organizationMapper.selectById(ledger.getObjectId());
            vo.setObjectName(organization == null ? "部门已被删除" : organization.getName());
        } else {
            SysUser user = sysUserMapper.selectById(ledger.getObjectId());
            vo.setObjectName(user == null ? "用户已被删除" : user.getName());
        }
        return vo;
    }
}
